<?php
	require_once 'base.dao.php';
	require_once 'group.vao.php';
	
	class GroupManager extends BaseManager {
		public function getGroupById ($groupId) {
			$query = sprintf("select * from groups where id = %d",
							 $groupId);
			try {
				$result = $this->executeQuery($query);
				if (count($result) == 0)
					return null;
				extract($result[0]);
				return new Group($id,
								   $name,
								   $description,
								   $mask,
								   $company_id);
			} catch (QueryException $e) {
				return null;
			}
		}
                public function getAllGroups(){
                    $query = "SELECT * FROM groups";
                    try {
                            $result = $this->executeQuery($query);
                            if (count($result) == 0)
                                    return null;
                            return $result;
                    } catch (QueryException $e) {
                            return null;
                    }
                }
                public function updateGroup($data){
                    if($msg=$this->validateData($data))return $msg;
                    $query = "UPDATE
                                groups
                              SET
                                name='".$data['name']."',
                                description='".$data['description']."',
                                mask='".$data['mask']."'
                              WHERE
                                id=".$_SESSION['groupId'];
//                    try {
                            $this->query($query);
//                    } catch (QueryException $e) {
//                            return "Hubo un problema al guardar.<br />$e";
//                    }
                }
                public function addGroup($data){
                    if($msg=$this->validateData($data))return $msg;
                    $query = "INSERT
                                groups
                              SET
                                name='".$data['name']."',
                                description='".$data['description']."',
                                mask='".$data['mask']."'";
//                    try {
                            $this->query($query);
//                    } catch (QueryException $e) {
//                            return "Hubo un problema al guardar.<br />$e";
//                    }
                }
                private function validateData($data){
                    if(!trim($data['name']))return "El nombre no puede estar vac&iacute;o";
                }
	}
?>